//
//  CarouseFigure.m
//  05-homework
//
//  Created by 殷殷明静 on 16/7/20.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "CarouseFigure.h"

@implementation CarouseFigure

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (CarouseFigure *)creatCarouseFigureWithFrame:(CGRect)frame
                             imageArray:(NSMutableArray *)imageArray {
    CarouseFigure *view =[[CarouseFigure alloc] initWithFrame:frame];
    view.tag = 1000;
    view.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    view.scroll.contentSize = CGSizeMake(frame.size.width * (imageArray.count + 2), 0);
    view.scroll.pagingEnabled = YES;
    view.scroll.contentOffset = CGPointMake(WIDTH, 0);
    for (int i = 0; i < imageArray.count; i++) {
        view.smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake((i + 1) * WIDTH, 0, WIDTH, HEIGHT)];
        view.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [view.smallScroll addSubview:view.imageView];
        view.smallScroll.maximumZoomScale = 2;
        view.smallScroll.minimumZoomScale = 0.5;
        [view.scroll addSubview:view.smallScroll];
        view.smallScroll.delegate = view;
        view.imageView.image = [UIImage imageNamed:imageArray[i]];
    }
    view.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    view.smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * 9, 0, WIDTH, HEIGHT)];
    [view.scroll addSubview:view.smallScroll];
    [view.smallScroll addSubview:view.imageView];
    view.smallScroll.delegate = view;
    view.smallScroll.maximumZoomScale = 2;
    view.smallScroll.minimumZoomScale = 0.5;
    view.imageView.image = [UIImage imageNamed:[imageArray firstObject]];
    view.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    view.smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [view.scroll addSubview:view.smallScroll];
    [view.smallScroll addSubview:view.imageView];
    view.smallScroll.delegate = view;
    view.smallScroll.maximumZoomScale = 2;
    view.smallScroll.minimumZoomScale = 0.5;
    view.imageView.image = [UIImage imageNamed:[imageArray lastObject]];
    [view addSubview:view.scroll];
    // pageControll
    view.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3 * 2, 37)];
    view.page.center = CGPointMake(WIDTH / 2, HEIGHT / imageArray.count * (imageArray.count - 1));
    [view addSubview:view.page];
    view.scroll.delegate = view;
    view.page.numberOfPages = imageArray.count;
    view.page.backgroundColor = [UIColor clearColor];
    view.page.currentPageIndicatorTintColor = [UIColor blackColor];
    [view.page addTarget:self action:@selector(change) forControlEvents:UIControlEventValueChanged];
    // zoom
    
    return view;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CarouseFigure *view = [self viewWithTag:1000];
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }

    if (scrollView.contentOffset.x == kWith * 9) {
        view.scroll.contentOffset = CGPointMake(kWith, 0);
    } else if (scrollView.contentOffset.x == 0) {
        view.scroll.contentOffset = CGPointMake(kWith * 8, 0);
    }
    view.page.currentPage = scrollView.contentOffset.x  / kWith - 1;
}
- (void)change {
    CarouseFigure *view = [self viewWithTag:1000];
    view.scroll.contentOffset = CGPointMake((self.page.currentPage + 1) * kWith, 0);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return scrollView.subviews.firstObject;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    
}
@end
